<template>
	<view class="gooddetails">
		
		<view class="search-box">
			<search-input ref="searchInput" placeholder="拆零扫码" @searchConfirm="handleSearch" rightText="清空" :isRight="true">
			</search-input>
		</view>
		<view class="panel" v-if="goodsDetails.Code">
			<view v-if="currentWareCode"  class="box">
				<view class="font_b size_18" style="margin-bottom: 10px;">当前选择药房:{{currentWareName}}</view>
			</view>
			<view class="box">
				<view class="flex-row box-row">
					<view class="left">
						<text>拆零比例</text>
					</view>
					<view class="right flex-row">					
						<input type="number" placeholder="请输入拆零比例" v-model="goodsDetails.AmountLeft" :isFocus="false" style="width: 100%;" />
					</view>
				</view>
			</view>
			<view class="box">
				<view class="flex-row box-row" style="margin-top: 0;">
					<view class="left">
						<text>商品编号</text>
					</view>
					<view class="right">
						<text>{{goodsDetails.Code}}</text>
					</view>
				</view>
				<view class="flex-row box-row">
					<view class="left">
						<text>商品名称</text>
					</view>
					<view class="right">
						<text>{{goodsDetails.Name}}</text>
					</view>
				</view>
				<view class="flex-row box-row">
					<view class="left">
						<text>生产商</text>
					</view>
					<view class="right">
						<text>{{goodsDetails.Manufacturer}}</text>
					</view>
				</view>
				<view class="flex-row box-row">
					<view class="left">
						<text>发票号码</text>
					</view>
					<view class="right">
						<text>{{goodsDetails.InvoiceCode}}</text>
					</view>
					<view class="left" style="margin-left: 10px;">
						<text>规格单位</text>
					</view>
					<view class="right">
						<text class="font_b">{{goodsDetails.Unit}}</text>
					</view>
				</view>
				
			</view>
			<view class="box">			
				<view class="flex-row box-row">
					<view class="left">
						<text>溯源码</text>
					</view>
					<view class="right">
						<text>{{searchVal}}</text>
					</view>
				</view>
			</view>
			<view class="flex-row footer-btn">
				<view class="common-btn" @tap="toHome">
					<text>首页</text>
				</view>
				<view class="white-btn" @tap="CleanAgainScan">
					<text>清空重扫</text>
				</view>			
				<view class="common-btn" @tap="saveCailing(1)">
					<text>确认</text>
				</view>
			</view>
		</view>
		
		
		<uni-popup ref="popupChoiceWareHouse" type="center" borderRadius="20rpx 20rpx 20rpx 20rpx" :mask-click="false">
			<view class="choice-Batch">
				<view class="choice-title">请选择药房</view>
				<view class="choice-panel">
					<view class="choice-item" @tap="ChoiceWareHouse(item)" v-for="item in warehouselist">{{item.PharmacyName}}</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import searchInput from '@/components/search-input/search-input.vue'
	var that;
	export default {
		components: {
			searchInput
		},
		data() {
			return {
				billId: '', // 单据Id
				BillCode: '', // 单据编码
				searchVal: '',				
				goodsDetails: {},
				statusHeight:0,
				isFocus:false,
				warehouselist:[] ,//药房列表
				currentWareCode:'',
				currentWareName:'',
				SupervisionCode:''			
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				stockInGoods: state => state.goods.stockInGoods, // stockInGoods
				scanCode: state => state.goods.scanCode, // scanCode				
			})
		},
		onLoad() {
			that = this			
			that.statusHeight = parseInt(uni.getSystemInfoSync().statusBarHeight)+10;
			that.SupervisionCode = that.$Route.query.SupervisionCode;
		},
		onShow() {
			//  如果从对码页面过来，直接取商品详情
			if (that.SupervisionCode)
			{
				that.currentWareCode = that.$Route.query.PharmacyCode
				that.getGoodDetails(that.SupervisionCode)
			}
			else
			{
				that.getWarehouselist();
			}
			
		},
		onUnload() {
			//that.cleanScanCode()
		},		
		methods: {
			...mapMutations(['initScanCode', 'cleanScanCode']),		
			handleSearch(res) {
				that.searchVal = res.value;						
				that.checkCailing(that.searchVal)								
			},
			
			//获取当前用户的药房列表
			getWarehouselist(){				
				that.$request.globalRequest('supplierstock/pharmacylist', 'POST', {},
					'', 0).then(res => {
					console.log(res)
					if (res.length>1)
					{
						that.warehouselist = res;
						that.$refs.popupChoiceWareHouse.open();
					}
					else if (res.length == 1)
					{
						that.currentWareCode = res[0].PharmacyCode;
						that.currentWareName = res[0].PharmacyName;
						that.$refs.searchInput.cleanInput()						
					}
					else
					{
						that.$common.showNormalToast('当前用户未绑定药房，请先绑定药房');	
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
					
				}).catch(res => {
					
				})
			},
			//保存拆零
			saveCailing(type,value){				
				let requestData = {
					"PharmacyCode": that.currentWareCode,
					"SupervisionCode": that.SupervisionCode,    
					"Code": that.goodsDetails.Code,
					"Name": that.goodsDetails.Name,
					"AmountTotal": that.goodsDetails.AmountTotal,
					"AmountLeft": that.goodsDetails.AmountLeft,
					"IsCanUse": that.goodsDetails.IsCanUse,
				}
				uni.showLoading({
					title: '提交中'
				})
				that.$request.globalRequest('goods/splitpackage', 'POST', requestData,
					'', 0).then(res => {						
					uni.hideLoading()	
					if (type == 1)
					{					
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
					else{
						that.getGoodDetails(value)
					}
					
				}).catch(res => {								
					that.$refs.searchInput.cleanInput();
					that.goodsDetails = {}					
					uni.hideLoading()
				})
			},
			
			checkCailing(value){
				if(that.goodsDetails.Code){
					that.saveCailing(0,value);
				}
				else{
					that.getGoodDetails(value)
				}
			},
			
			//获取商品详情
			getGoodDetails(value){						
				that.$request.globalRequest('goods/splitgoodslist', 'POST', value,
					'', 0).then(res => {
					//console.log(res);
					if (!res.Code)
					{
						uni.showModal({
							title: '提示',
							confirmText: '去对码',
							content: '该溯源码未绑定商品',
							success: function(res) {
								if (res.confirm) {
									let url = {
										path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList', 
										query: { 
											SupplierCode: '',
											SupervisionCode: value,
											scanType:4,
											PharmacyCode:that.currentWareCode
										}
									}
									that.$Router.push(url)
								} else if (res.cancel) {
									
								}
							}
						});
					}
					that.goodsDetails = res;
					that.SupervisionCode = that.searchVal;
					that.$refs.searchInput.cleanInput();
					
				}).catch(res => {
					
				})
			},
			//选择药房
			ChoiceWareHouse(item){
				that.currentWareCode = item.PharmacyCode;
				that.currentWareName = item.PharmacyName;
				that.$refs.popupChoiceWareHouse.close();
				that.$refs.searchInput.cleanInput()	
			},
			CleanAgainScan(){
				that.$refs.searchInput.cleanInput()	
			},
			toHome() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			//清除重扫
			handleCleanSearch() {
				that.$refs.searchInput.cleanInput()	
			},				
		}
	}
</script>

<style lang="scss" scoped>
	@import "cailing.scss";
</style>
